﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Auth_Usuarios : System.Web.UI.UserControl, IViewStatePersist
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void gvListagem_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.dvCadastro.PageIndex = e.NewEditIndex;
        this.dvCadastro.ChangeMode(DetailsViewMode.Edit);
        this.gvListagem.SelectedIndex = e.NewEditIndex;
        e.NewEditIndex = -1;
    }
    protected void dvCadastro_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if ("Cancel".Equals(e.CommandName))
        {
            gvListagem.SelectedIndex = -1;
        }
    }
    protected void dvCadastro_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            gvListagem.SelectedIndex = -1;
        }
    }

    protected void cvSenhas_ServerValidate(object source, ServerValidateEventArgs args)
    {
        TextBox txtSenha1 = dvCadastro.FindControl("txtSenha1") as TextBox;
        TextBox txtSenha2 = dvCadastro.FindControl("txtSenha2") as TextBox;

        args.IsValid = txtSenha1.Text.Equals(txtSenha2.Text);

    }
    protected void dvCadastro_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        CustomValidator cvSenhas = dvCadastro.FindControl("cvSenhas") as CustomValidator;
        cvSenhas.Validate();
        e.Cancel = !cvSenhas.IsValid;
    }
    protected void dvCadastro_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        CustomValidator cvSenhas = dvCadastro.FindControl("cvSenhas") as CustomValidator;
        cvSenhas.Validate();
        e.Cancel = !cvSenhas.IsValid;
    }
    protected void gvListagem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblTipoUsuario = e.Row.FindControl("lblTipoUsuario") as Label;
            DataRowView drv = e.Row.DataItem as DataRowView;
            if ("A".Equals(drv["UsuarioTipo"]))
            {
                lblTipoUsuario.Text = "Administrador";
            }
            else
            {
                lblTipoUsuario.Text = "Operário";
            }
        }
    }

	#region IViewStatePersist Members

	public new object SaveViewState()
	{
		return base.SaveViewState();
	}

	public new void LoadViewState(object savedState)
	{
		base.LoadViewState(savedState);
	}

	#endregion
}
